﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using PhysicalDataProvider;
using FooFun.DataElements.Dtos;

namespace FooFun.Faq.Dal.Services
{
    public class RecommendIndexService : ITopicService
    {
        DEDataProvider dataProvider;

        public DEDataProvider DataProvider
        {
            get { return dataProvider; }
            set { dataProvider = value; }
        }

        public void FixTopic(Guid forumId, Guid topicId)
        {
            DbParameter para1 = DataProvider.CreateParameter();
            DbParameter para2 = DataProvider.CreateParameter();

            para1.Value = DataProviderUtil.GetGuidBase64(forumId);
            para2.Value = DataProviderUtil.GetGuidBase64(topicId);

            DataProvider.ExecuteNonQuery(@"DELETE FROM t_Recommends
WHERE TopicId = ?", para2);

            DataProvider.ExecuteNonQuery(@"INSERT INTO t_Recommends
(ForumId,TopicId,TopicNo)
VALUES
(?,?,0)", para1, para2);

        }

        public IDataElement ListRecommends(Guid forumId)
        {
            IDataElement de
                = DataProvider.Select(new string[] { "TopicId" },
                  "t_Recommends",
                  string.Format(
                  "ForumId='{0}' ORDER BY TopicNo",
                  DataProviderUtil.GetGuidBase64(forumId)
                  )
                  );

            return de;
        }

        #region ITopicService 成员

        public void CreateTopic(string topicTitle, string remark, Guid eventId, Guid forumId, Guid userId)
        {
            return;
        }

        public void MoveTopic(IDataElement topic, Guid forumId)
        {
            DbParameter para2 = DataProvider.CreateParameter();

            para2.Value = DataProviderUtil.GetGuidBase64(
                topic["@TopicId"].Value);

            DataProvider.ExecuteNonQuery(@"DELETE FROM t_Recommends
WHERE TopicId = ?", para2);
        }

        public void RemoveTopic(Guid topicId)
        {
            DbParameter para2 = DataProvider.CreateParameter();

            para2.Value = DataProviderUtil.GetGuidBase64(topicId);

            DataProvider.ExecuteNonQuery(@"DELETE FROM t_Recommends
WHERE TopicId = ?", para2);
        }

        #endregion

        public void RemoveFixTopic(Guid topicId)
        {
            DbParameter para1 = DataProvider.CreateParameter();

            para1.Value = DataProviderUtil.GetGuidBase64(topicId);

            DataProvider.ExecuteNonQuery(@"DELETE FROM t_Recommends
WHERE TopicId = ?", para1);
        }
    }
}
